<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-input
              size="medium"
              v-model="subquery"
              placeholder="查询"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="onSubmit"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <div>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column
              prop="reimbursementPerson"
              label="报销人"
              width=""
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="department"
              label="所属部门"
              width=""
              align="center"
            >
            </el-table-column>
            <el-table-column prop="date" label="日期" align="center">
            </el-table-column>
            <el-table-column
              prop="reimbursementInstructions"
              label="报销说明"
              align="center"
            >
            </el-table-column>

            <el-table-column label="进度" align="center">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.flowState == 1"
                  >已付款</el-tag
                >
                <el-tag type="warning" v-if="scope.row.flowState == 0"
                  >进行中</el-tag
                >
                <el-tag type="danger" v-if="scope.row.flowState == 3"
                  >拒绝</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column label="操作" width="240" align="center">
              <template #default="scope">
                <el-button
                  size="mini"
                  type="success"
                  v-if="scope.row.isCanApproval == 1"
                  @click="examine(scope.row, 3)"
                  >审核</el-button
                >
                <el-button
                  type="warning"
                  size="mini"
                  @click="handleEdit(scope.row, 2)"
                  >查看详情</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="pagingClass">
            <el-pagination
              :page-sizes="m_page.sizes"
              :page-size="m_page.size"
              :current-page="m_page.number"
              @size-change="m_handleSizeChange"
              @current-change="m_handlePageChange"
              layout="total,sizes,prev, pager, next"
              background
              :total="m_page.totalElements"
            ></el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-dialog
      :title="single"
      v-model="plassets"
      width="75%"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="报销人">
          <el-select
            @change="peopleknew"
            v-model="form.people"
            style="width: 300px"
            filterable
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in subbuyer"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门">
          <el-cascader
            :show-all-levels="false"
            style="width: 300px"
            clearable
            v-model="form.superior"
            :options="department"
            :props="{ checkStrictly: true }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            style="width: 300px"
            v-model="form.dates"
            type="date"
            placeholder="选择日期"
            format="YYYY 年 MM 月 DD 日"
          >
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报销明细" style="width: 100%">
          <el-table :data="detailList" show-summary border style="width: 100%">
            <el-table-column label="内容" width="250">
              <template #default="scope">
                <el-input
                  type="textarea"
                  :rows="2"
                  v-model="scope.row.content"
                  placeholder="内容"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="报销金额" width="150">
              <template #default="scope">
                <el-input
                  type="number"
                  v-model.number="scope.row.amount"
                  placeholder="报销金额"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="发生日期" width="200">
              <template #default="scope">
                <el-date-picker
                  style="width: 170px"
                  v-model="scope.row.date"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY 年 MM 月 DD 日"
                >
                </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="项目号" width="220">
              <template #default="scope">
                <el-input
                  style="width: 150px"
                  v-model="scope.row.projectNumber"
                  placeholder="请输入内容"
                ></el-input>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="subitem(scope.$index)"
                ></el-button>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="备注" width="200">
              <template #default="scope">
                <el-input
                  type="textarea"
                  :rows="2"
                  v-model="scope.row.remarks"
                  placeholder="备注"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="addnote" label="上传图片" width="200">
              <el-upload
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog v-model="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template #default="scope">
                <el-button
                  type="text"
                  @click="deleteunused(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="报销说明">
          <el-input
            style="width: 300px"
            v-model="form.pseudo"
            type="textarea"
            :rows="2"
          ></el-input>
        </el-form-item>
        <el-form-item label="审批进度" v-if="under == 2">
          <div style="height: 300px; margin-left: 30px">
            <el-steps
              direction="vertical"
              :active="stripes"
              v-if="passive.length"
            >
              <el-step
                v-for="item in passive"
                :title="item.nodeName"
                :description="item.remarks"
                :key="item.id"
              ></el-step>
            </el-steps>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" style="text-align: center">
          <el-button type="success" @click="Disagree()" v-if="under == 3"
            >同 意</el-button
          >
          <el-button type="danger" @click="refused()" v-if="under == 3"
            >拒 绝</el-button
          >
          <el-button @click="plassets = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="拒绝理由" v-model="dynnetobjs" width="25%">
      <el-form ref="form" :model="restore" label-width="80px">
        <el-form-item label="拒绝理由">
          <el-input
            style="width: 300px"
            v-model="restore.ofreasons"
            type="textarea"
            :rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dynnetobjs = false">取 消</el-button>
          <el-button type="primary" @click="addition()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      restore: {
        ofreasons: "",
      },
      under: 2,
      single: "财务报销",
      plassets: false,
      subquery: "",
      dialogImageUrl: "",
      dialogVisible: false, //上传图片
      dynnetobjs: false,

      form: {
        people: "", //报销人
        superior: "", //所属部门
        dates: "",
        pseudo: "", //说明
      },
      peoplechin: "", //报销人中文
      department: [], //部门
      editors: "", //部门ID
      superiorchin: "", //部门中文
      subbuyer: [], //人员
      parent: [],
      stripes: "",
      passive: [],
      detailList: [
        {
          id: "",
          content: "", //内容
          amount: "", //金额
          date: "", //日期
          projectId: "",
          projectNumber: "",
          remarks: "", //备注
          file: "", //图片
        },
      ],
      tableData: [],
      m_page: {
        sizes: [10, 20, 40, 50, 100], //每页最大显示数
        size: 10,
        totalElements: 0,
        totalPages: 3,
        number: 1,
      },
      headerObj: "",
      underID: "",
      approvers: {
        approvalIds: "",
        approvalState: "",
        approvalers: "",
        createId: "",
        createTime: "",
        id: "",
        isEffective: "",
        nodeName: "",
        nodeType: "",
        pId: "",
        processdesigndetailsId: "",
        sort: "",
        tableName: "",
        updateTime: "",
      },
    };
  },
  created() {
    let str = sessionStorage.getItem("token");
    this.headerObj = `Bearer ${str}`;
  },
  beforeMount() {
    this.production();
    this.Acquisition();
    this.getRecord();
  },
  methods: {
    //项目号
    subitem(index) {
      this.csListIndex = index;

      this.nserialnos = true;
    },
    addition() {
      this.$axios
        .post(
          `api/serial/ApprovalHistory`,
          {
            pId: this.underID,
            nodeName: this.approvers.nodeName,
            tableName: this.approvers.tableName,
            nodeType: this.approvers.nodeType,
            approvalIds: this.approvers.approvalIds,
            isEffective: this.approvers.isEffective,
            processdesigndetailsId: this.approvers.processdesigndetailsId,
            approvalState: 3,
            approvalers: this.approvers.approvalers,
            sort: this.approvers.sort,
            id: this.approvers.id,
            remarks: this.restore.ofreasons,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          this.$message({
            message: "拒绝",
            type: "success",
          });
          this.getRecord();
          this.plassets = false;
          this.dynnetobjs = false;
        });
    },
    //拒绝理由
    refused() {
      this.dynnetobjs = true;
    },
    onSubmit() {},
    //同意
    Disagree() {
      this.$axios
        .post(
          `api/serial/ApprovalHistory`,
          {
            pId: this.underID,
            nodeName: this.approvers.nodeName,
            tableName: this.approvers.tableName,
            nodeType: this.approvers.nodeType,
            approvalIds: this.approvers.approvalIds,
            isEffective: this.approvers.isEffective,
            processdesigndetailsId: this.approvers.processdesigndetailsId,
            approvalState: 1,
            approvalers: this.approvers.approvalers,
            sort: this.approvers.sort,
            id: this.approvers.id,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          this.$message({
            message: "同意",
            type: "success",
          });
          this.getRecord();
          this.plassets = false;
        });
    },
    examine(row, index) {
      this.plassets = true;
      this.under = index;
      this.single = "审核";
      this.underID = row.id;
      this.getProject();
      this.scrollTop();
    },
    //查看详情
    handleEdit(row, index) {
      this.underID = row.id;
      this.passive = [];
      this.plassets = true;
      this.under = index;
      this.single = "查看详情";
      this.getProject();
      this.scrollTop();
    },
    scrollTop() {
      this.$axios({
        methods: "GET",
        url: "api/serial/Reimbursement",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: 10,
          pageIndex: 1,
          query: {
            id: this.underID,
          },
        },
      }).then((res) => {
        this.choose = res.data.data;
        console.log(this.choose);
        this.form.people = this.choose.reimbursementPersonId;
        this.form.superior = this.choose.departmentId;
        this.editors = this.choose.departmentId;
        this.form.dates = this.choose.date;

        this.detailList = this.choose.reimbursementDetails;
        this.form.pseudo = this.choose.reimbursementInstructions;
      });
    },
    //获取流程
    getProject() {
      this.$axios({
        methods: "GET",
        url: "api/serial/ApprovalHistory",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: 20,
          pageIndex: 1,
          query: {
            pId: this.underID,
            tableName: "reimbursement",
          },
        },
      })
        .then((res) => {
          console.log(res);
          this.approvers = res.data.data[2];
          // console.log(this.approvers);
          this.passive = res.data.data;

          let term = this.passive.reduce((count, item) => {
            console.log(item);
            if (item.approvalState == 1 || item.approvalState == 3) {
              return count + 1;
            }
            return count;
          }, 0);
          // console.log(term);
          this.stripes = term;
          // this.passive.forEach((item) => {
          //   term.push(item.approvalState);
          // });

          // this.stripes = term.reduce((pre, cur) => pre + cur, 0);
        })
        .catch(() => {
          this.$message.error("获取失败");
        });
    },
    //获取数据
    getRecord() {
      this.$axios({
        methods: "GET",
        url: "api/serial/Reimbursement",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: this.m_page.size,
          pageIndex: this.m_page.number,
          query: {
            IsToFinance: 1,
            categoryName: this.foodName,
          },
        },
      }).then((res) => {
        console.log(res);
        ({ data: this.tableData, count: this.m_page.totalElements } = res.data);
        // this.loadFlag = false;
      });
    },
    peopleknew(value) {
      this.subbuyer.forEach((item) => {
        if (value == item.value) {
          // console.log(item);
          this.peoplechin = item.label;
        }
      });
      console.log(this.peoplechin);
    },
    //获取人员
    production() {
      this.$axios
        .get(`api/serial/Users?pageSize=${100}&pageIndex=${1}`, {
          headers: {
            Authorization: this.headerObj,
          },
        })
        .then((res) => {
          this.dateEnd = res.data.data;
          let fwork = [];
          this.dateEnd.forEach((item) => {
            fwork.push({
              departId: item.departId,
              value: item.id,
              label: item.name,
            });
          });
          // console.log(fwork);
          this.subbuyer = fwork;
        });
    },
    //获取上级部门
    Acquisition() {
      this.$axios
        .get(`api/serial/DepartmentTree`, {
          headers: {
            Authorization: this.headerObj,
          },
        })
        .then((res) => {
          // console.log(res);
          this.fication = res.data.data;
          // console.log(this.fication);
          let arr = [];
          this.fication.forEach((item, index) => {
            arr[index] = {
              value: item.id,
              label: item.departName,
            };

            if (item.departmentList.length) {
              arr[index].children = [];
              item.departmentList.forEach((item1, index1) => {
                arr[index].children[index1] = {
                  value: item1.id,
                  label: item1.departName,
                };
                arr[index].children[index1].children = [];
                if (item1.departmentList.length) {
                  item1.departmentList.forEach((item2, index2) => {
                    arr[index].children[index1].children[index2] = {
                      value: item2.id,
                      label: item2.departName,
                    };
                  });
                }
              });
            }
          });

          this.department = arr;
        });
    },
    handleChange() {
      if (this.form.superior.length == 1) {
        this.editors = this.form.superior[0];
      } else if (this.form.superior.length == 2) {
        this.editors = this.form.superior[1];
      } else if (this.form.superior.length == 3) {
        this.editors = this.form.superior[2];
      }
      console.log(this.editors);
      this.department.forEach((item) => {
        console.log(item);
        if (item.children) {
          item.children.forEach((item1) => {
            if (this.editors == item1.value) {
              this.superiorchin = item1.label;
            }
          });
        }
      });
      // console.log(this.superiorchin);
    },
    //上传图片
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //页码
    m_handlePageChange(currPage) {
      this.m_page.number = currPage;
      this.getProducts();
    },
    m_handleSizeChange(currSize) {
      this.m_page.size = currSize;
      this.getProducts();
    },
  },
};
</script>

<style>
.addccc {
  margin-bottom: 20px;
}
element.style {
  margin-top: 7vh important;
}
</style>